package com.rfs.juc;

import javax.xml.stream.events.EndDocument;


/**
 * 并发性测试
 *
 * @author renfushuai
 * @date 2022/12/05
 */
public class ConcurrencyTest {
    private static final long count=10000;

    public static void main(String[] args) throws InterruptedException {
        concurrency();
        serial();
    }

    /**
     * 并发性
     *
     * @throws InterruptedException 中断异常
     */
    private static void concurrency() throws InterruptedException{
        long start = System.currentTimeMillis();
        Thread t = new Thread(() -> {
            int a = 0;
            for (long i = 0; i < count; i++) {
                a += 5;
            }
        });
        t.start();
        int b=0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long time = System.currentTimeMillis()-start;
        t.join();
        System.out.println("currency:"+time+"ms,b="+b);
    }

    /**
     * 串行
     */
    private static void serial(){
        long start = System.currentTimeMillis();
        int a=0;
        for (long i = 0; i < count; i++) {
            a+=5;
        }
        int b=0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long time=System.currentTimeMillis()- start;
        System.out.println("serial:"+time+"ms,b="+b);
    }
}
